package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService{
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void saveOrUpdate(StrategyComment entity,String[] tags) {
        if (entity.getId() == null) {
            //设置评论时间
            entity.setCreateTime(new Date());
            strategyCommentMapper.insert(entity);
            //保存标签信息到数据库中
            for (String temp : tags) {
                Tag tag = new Tag();
                tag.setName(temp);
                tagMapper.insert(tag);
            //关联评论和中间表信息
            strategyCommentMapper.insertRelation(entity.getId(),tag.getId());
            }


        }else{
            strategyCommentMapper.updateByPrimaryKey(entity);

        }
    }

    @Override
    public PageInfo list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo<>(list);
        
    }

    @Override
    public StrategyComment selectById(Long id) {
        return strategyCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyComment> selectAll() {
        return strategyCommentMapper.selectAll();
    }

    @Override
    public List<String> queryTagsByStrategyId(Long strategyId, Integer size) {
        return strategyCommentMapper.selectTagsByStrategyId(strategyId,size);
    }

    @Override
    public PageInfo queryCommentsByUserId(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

}
